package com.assignwork.service.impl;

import com.assignwork.dto.WorkerInfoDTO;
import com.assignwork.vo.WorkerInfoVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.common.cache.CacheNames;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;
import org.springblade.core.mp.base.BaseServiceImpl;
import com.assignwork.entity.WorkerInfo;
import com.assignwork.mapper.WorkerInfoMapper;
import com.assignwork.service.IWorkerInfoService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 工人信息(WorkerInfo)表服务实现类
 */
@AllArgsConstructor
@Service("workerInfoService")
public class WorkerInfoServiceImpl extends BaseServiceImpl<WorkerInfoMapper, WorkerInfo> implements IWorkerInfoService {

    @Override
    public IPage<WorkerInfoVO> pageWorkerInfo(BladeUser bladeUser, IPage<WorkerInfo> page, WorkerInfoDTO dto) {
        StringBuffer sql = new StringBuffer("order by");
        if (Func.isNotEmpty(dto.getRank())) {
            sql.append(" id in (");
            sql.append(Func.join(dto.getRank()));
            sql.append(") desc, ");
        }
        sql.append(" team_key asc, real_name asc ");
        IPage<WorkerInfo> page1 = this.lambdaQuery()
                .and(Func.isNotBlank(dto.getKeyword()), q -> {
                    q.or().like(WorkerInfo::getRealName, dto.getKeyword());
                    q.or().like(WorkerInfo::getNickname, dto.getKeyword());
                    q.or().like(WorkerInfo::getJobNo, dto.getKeyword());
                })
                .eq(Func.isNotBlank(dto.getGroupKey()), WorkerInfo::getGroupKey, dto.getGroupKey())
                .eq(Func.isNotBlank(dto.getTeamKey()), WorkerInfo::getTeamKey, dto.getTeamKey())
                .last(sql.toString())
                .page(page);

        List<WorkerInfoVO> res = new ArrayList<>();
        for (WorkerInfo record : page1.getRecords()) {
            WorkerInfoVO vo = new WorkerInfoVO();
            Func.copy(record, vo);
            vo.setGenderName(DictBizCache.getValue(CacheNames.DICT_GENDER, vo.getPostKey()));
            vo.setPostName(DictBizCache.getValue(CacheNames.DICT_ASSIGN_WORK_POST, vo.getPostKey()));
            vo.setGroupName(DictBizCache.getValue(CacheNames.DICT_MACHINE_GROUP, vo.getGroupKey()));
            vo.setTeamName(DictBizCache.getValue(CacheNames.DICT_MACHINE_TEAM, vo.getTeamKey()));
            vo.setEmployeeTypeName(DictBizCache.getValue(CacheNames.DICT_EMPLOYEE_TYPE, vo.getEmployeeType()));
            vo.setSalaryTypeName(DictBizCache.getValue(CacheNames.DICT_SALARY_TYPE, vo.getSalaryType()));
            vo.setShiftTypeName(DictBizCache.getValue(CacheNames.DICT_SHIFT_TYPE_KEY, vo.getShiftTypeKey()));

            res.add(vo);
        }
        return Condition.getPage(page1, res);
    }

    @Override
    public List<WorkerInfoVO> listWorkerInfo(BladeUser bladeUser, WorkerInfoDTO dto) {
        List<WorkerInfo> list = this.lambdaQuery()
                .and(Func.isNotBlank(dto.getKeyword()), q -> {
                    q.or().like(WorkerInfo::getRealName, dto.getKeyword());
                    q.or().like(WorkerInfo::getNickname, dto.getKeyword());
                    q.or().like(WorkerInfo::getJobNo, dto.getKeyword());
                })
                .eq(Func.isNotBlank(dto.getGroupKey()), WorkerInfo::getGroupKey, dto.getGroupKey())
                .eq(Func.isNotBlank(dto.getTeamKey()), WorkerInfo::getTeamKey, dto.getTeamKey())
                .orderByDesc(WorkerInfo::getRealName)
                .list();

        if (list == null) {
            return Collections.emptyList();
        }

        List<WorkerInfoVO> res = Func.copy(list, WorkerInfoVO.class);
        for (WorkerInfoVO vo : res) {
//            vo.setGenderName(DictBizCache.getValue(CacheNames.DICT_GENDER, vo.getPostKey()));
//            vo.setPostName(DictBizCache.getValue(CacheNames.DICT_ASSIGN_WORK_POST, vo.getPostKey()));
            vo.setGroupName(DictBizCache.getValue(CacheNames.DICT_MACHINE_GROUP, vo.getGroupKey()));
            vo.setTeamName(DictBizCache.getValue(CacheNames.DICT_MACHINE_TEAM, vo.getTeamKey()));
//            vo.setEmployeeTypeName(DictBizCache.getValue(CacheNames.DICT_EMPLOYEE_TYPE, vo.getEmployeeType()));
//            vo.setSalaryTypeName(DictBizCache.getValue(CacheNames.DICT_SALARY_TYPE, vo.getSalaryType()));
//            vo.setShiftTypeName(DictBizCache.getValue(CacheNames.DICT_SHIFT_TYPE_KEY, vo.getShiftTypeKey()));
        }

        return res;
    }

}
